package com.service.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

/**
 * @ClassName XmlUtils
 * @Description TODO
 * @Author Cherish
 * @Date 2019/9/3 17:14
 * @Version 1.0
 */
public class XmlUtils {

    public static Map<String, Object> xmlToMap(String xml) throws Exception{
        try {
            Document doc = DocumentHelper.parseText(xml);
            Map<String, Object> map = new HashMap<String, Object>();
            if(doc == null)
                return map;
            Element root = doc.getRootElement();
            System.out.println(root.getText());
            for (@SuppressWarnings("unchecked")
                 Iterator iterator = root.elementIterator(); iterator.hasNext();) {
                Element e = (Element) iterator.next();
                @SuppressWarnings("unchecked")
                List list = e.elements();
                if(list.size() > 0){
                    map.put(e.getName(), Dom2Map(e));
                }else
                    map.put(e.getName(), e.getText());
            }
            return map;
        } catch (DocumentException e) {
            throw e;
        }
    }
    public static Map Dom2Map(Element e) throws Exception{
        try {
            Map map = new HashMap();
            @SuppressWarnings("unchecked")
            List list = e.elements();
            if(list.size() > 0){
                for (int i = 0;i < list.size(); i++) {
                    Element iter = (Element) list.get(i);
                    List mapList = new ArrayList();

                    if(iter.elements().size() > 0){
                        Map m = Dom2Map(iter);
                        if(map.get(iter.getName()) != null){
                            Object obj = map.get(iter.getName());
                            if(!obj.getClass().getName().equals("java.util.ArrayList")){
                                mapList = new ArrayList();
                                mapList.add(obj);
                                mapList.add(m);
                            }
                            if(obj.getClass().getName().equals("java.util.ArrayList")){
                                mapList = (List) obj;
                                mapList.add(m);
                            }
                            map.put(iter.getName(), mapList);
                        }else
                            map.put(iter.getName(), m);
                    }
                    else{
                        if(map.get(iter.getName()) != null){
                            Object obj = map.get(iter.getName());
                            if(!obj.getClass().getName().equals("java.util.ArrayList")){
                                mapList = new ArrayList();
                                mapList.add(obj);
                                mapList.add(iter.getText());
                            }
                            if(obj.getClass().getName().equals("java.util.ArrayList")){
                                mapList = (List) obj;
                                mapList.add(iter.getText());
                            }
                            map.put(iter.getName(), mapList);
                        }else
                            map.put(iter.getName(), iter.getText());
                    }
                }
            }else{
                map.put(e.getName(), e.getText());
            }
            return map;
        } catch (Exception e2) {
            throw e2;
        }

    }

}
